//
//  YZMBankCardListCell.m
//  YZMicroStock
//
//  Created by Lv.Dongdong on 16/7/12.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMBankCardListCell.h"
#import <ColorUtils/ColorUtils.h>


@interface YZMBankCardListCell ()

@property (nonatomic, strong) UIView *contentBgView;
@property (nonatomic, strong) UIView *bankAddView;
@property (nonatomic, strong) UIImageView *bankIcon;
@property (nonatomic, strong) UILabel *bankName;
@property (nonatomic, strong) UILabel *bankNumber;
@property (nonatomic, strong) NSArray *colorArray;

@end

@implementation YZMBankCardListCell
{
    CAGradientLayer *layer;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self createObjects];
    }
    
    return self;
}

- (void)createObjects {
    
    WS(ws)
    self.contentBgView = [[UIView alloc] init];
    self.contentBgView.hidden = YES;
    self.contentBgView.layer.cornerRadius = 15;
    self.contentBgView.layer.masksToBounds = YES;
    self.contentBgView.backgroundColor=[UIColor clearColor];
    self.contentBgView.backgroundColor=[YZMAppStyle colorForBankRed];
    [self addSubview:self.contentBgView];
    
    [self.contentBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.equalTo(ws);
        make.left.equalTo(ws).offset(15);
        make.right.equalTo(ws).offset(-15);
    }];
    
    layer = [CAGradientLayer new];
    [self.contentBgView.layer addSublayer:layer];
    
    UIView *whiteBgView = [[UIView alloc] init];
    whiteBgView.backgroundColor = [YZMAppStyle colorForWhite];
    whiteBgView.layer.cornerRadius = 32 / 2;
    [self.contentBgView addSubview:whiteBgView];
    [whiteBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(ws.contentBgView).offset(15);
        make.top.equalTo(ws.contentBgView).offset(10);
        make.height.width.mas_equalTo(32);
    }];
    
    self.bankIcon = [[UIImageView alloc] init];
    [self.contentBgView addSubview:self.bankIcon];
    [self.bankIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.center.equalTo(whiteBgView);
    }];
    
    self.bankName = [YZMAppStyle labelWithWhite];
    [self.contentBgView addSubview:self.bankName];
    [self.bankName mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(whiteBgView);
        make.left.equalTo(whiteBgView.mas_right).offset(6);
    }];
    
    self.bankNumber = [YZMAppStyle labelWithWhite];
    self.bankNumber.font = [UIFont systemFontOfSize:26];
    self.bankNumber.adjustsFontSizeToFitWidth = YES;
    self.bankNumber.minimumScaleFactor = 0.1;
    [self.contentBgView addSubview:self.bankNumber];
    [self.bankNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(ws.bankName.mas_left);
        make.bottom.equalTo(ws.contentBgView).offset(-15);
        make.right.equalTo(ws.contentBgView).offset(-15);
    }];
    
    self.bankAddView = [[UIView alloc] init];
    self.bankAddView.hidden = YES;
    self.bankAddView.backgroundColor = [UIColor clearColor];
    self.bankAddView.layer.cornerRadius = 15;
    self.bankAddView.layer.borderWidth = 1;
    self.bankAddView.layer.borderColor = [YZMAppStyle colorForGrayUnable].CGColor;
    [self addSubview:self.bankAddView];
    [self.bankAddView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.equalTo(ws);
        make.left.equalTo(ws).offset(15);
        make.right.equalTo(ws).offset(-15);
    }];
    
    UIView *centerView = [[UIView alloc] init];
    [self.bankAddView addSubview:centerView];
    
    UIImageView *plusIcon = [[UIImageView alloc] init];
    plusIcon.image = [UIImage imageNamed:@"icon_plus"];
    [centerView addSubview:plusIcon];
    [plusIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(centerView);
        make.height.width.mas_equalTo(32);
    }];
    
    UILabel *titleName = [YZMAppStyle labelWithFont:[YZMAppStyle largeSizeFont] andColor:[YZMAppStyle colorForGrayUnable]];
    titleName.text = @"添加银行卡";
    [centerView addSubview:titleName];
    [titleName mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(plusIcon);
        make.left.equalTo(plusIcon.mas_right).offset(6);
    }];
    
    [centerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(plusIcon.mas_left);
        make.right.equalTo(titleName.mas_right);
        make.top.bottom.equalTo(plusIcon);
        make.center.equalTo(ws.bankAddView);
    }];
    
    self.backgroundView = nil;
    self.backgroundColor = [YZMAppStyle appBackgroundColor];
}

- (void)setIndexPath:(NSIndexPath *)indexPath {
    
    _indexPath = indexPath;
    if (self.indexPath.section%3==0) {
        
        self.colorArray=@[[UIColor colorWithString:@"#A6D4FA"],[UIColor colorWithString:@"#4D7BCF"]];
        
    } else if (self.indexPath.section%3==1) {
        
        self.colorArray=@[[UIColor colorWithString:@"#FAA6C7"],[UIColor colorWithString:@"#984DCF"]];
        
    } else {
        self.colorArray=@[[UIColor colorWithString:@"#FAA6BB"],[UIColor colorWithString:@"#CF4D4D"]];
    }
}

- (void)setItem:(YZMBankCardModel *)item {
    
    if(item) {
        
        UIColor *frontcolor=[self.colorArray objectAtIndex:0];
        UIColor *latercolor=[self.colorArray objectAtIndex:1];
        
        //设置渐变区域的起始和终止位置（范围为0-1）
        layer.startPoint = CGPointMake(0, 0);
        layer.endPoint = CGPointMake(0, 1);
        
        //设置颜色分割点（范围：0-1）
        layer.locations = @[@(0.0f), @(1.0f)];
        
        layer.colors = @[(__bridge id)frontcolor.CGColor, (__bridge id)latercolor.CGColor];
        [self.contentBgView layoutIfNeeded];
        
        layer.frame = self.contentBgView.bounds;
    }
    
    //添加内容
    _item = item;
    if(!item) {
        self.bankAddView.hidden = NO;
        self.contentBgView.hidden = YES;
        
        return;
    }

    self.bankAddView.hidden = YES;
    self.contentBgView.hidden = NO;
    self.bankIcon.image = [item iconImage];
    self.bankName.text = [item formatdefaultCardName];
    self.bankNumber.text = item.formatNumber;
}

@end
